package com.tutor.lzm.function.teacher.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.tutor.lzm.R;
import com.tutor.lzm.activity.BaseActivity;
import com.tutor.lzm.bean.SelectTime;
import com.tutor.lzm.bean.TimeType;
import com.tutor.lzm.function.teacher.adapter.GridSelectTimeAdapter;
import com.tutor.syslib.Logs;
import com.tutor.syslib.view.MyGridView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zzgpc on 2017-10-6.
 * 选择授课时间
 */
public class TeacherTimeAct extends BaseActivity {
    @BindView(R.id.gridView_select_time)
    MyGridView gridViewSelectTime;
    GridSelectTimeAdapter adapter;

    public static void start(Context context) {
        Intent intent = new Intent(context, TeacherTimeAct.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_teacher_time);
        ButterKnife.bind(this);
        initTitle("选择授课时间段", "确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<SelectTime> times = getTimes();
                Logs.d(tag, "times大小" + times.size());
                EventBus.getDefault().post(times);
                finish();
            }
        });
        adapter = new GridSelectTimeAdapter(this);
        adapter.setClick(true);
        gridViewSelectTime.setAdapter(adapter);
    }


    /**
     * 获取时间段
     * 一共只有7天。按照星期计算
     */
    private List<SelectTime> getTimes() {
        List<SelectTime> timeList = new ArrayList<>();
        boolean[] selectPosFlag = adapter.getSelectPosFlag();
        //循环获取选中的条目
        for (int i = 0; i < 24; i++) {
            boolean flag = selectPosFlag[i];
            if (flag) {

                int week = i % 8;
                int timeTypePos = (i / 8) + 1;
                boolean isHasPos = false;
                for (int r = 0; r < timeList.size(); r++) {
                    //如果已经保存有了当天的位置,获取位置地址
                    SelectTime selectPosTime = timeList.get(r);
                    if (selectPosTime.weekType == week) {
                        TimeType timeType = new TimeType();
                        timeType.timeType = timeTypePos;
                        Logs.d(tag, "add   timeType");
                        selectPosTime.timeTypeList.add(timeType);
                        isHasPos = true;
                        break;
                    }
                }
                //如果是当天第一条记录
                if (!isHasPos) {
                    SelectTime selectTime = new SelectTime();
                    selectTime.weekType = week;
                    TimeType timeType = new TimeType();
                    timeType.timeType = timeTypePos;
                    selectTime.timeTypeList.add(timeType);
                    timeList.add(selectTime);
                }
            }
        }

        return timeList;
    }
}
